//index.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')

var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page({
    data: {
        //wxtabs
        tabs: ["新房", "二手房", "租房"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,

        //二手房
        priceSelectInfo: {
            totalPrice: 1001,
            totalPriceShow: '不限'
        },
        hxSelectInfo: [false, false, false, false, false, false],
        xqpSelectInfo: [false, false, false, false, false, false],
        cxSelectInfo: [false, false, false, false, false],
        tsSelectInfo: [false, false, false, false],
        qySelectInfo: [false, false, false, false, false, false, false, false, false],
        //新房
        lxSelectInfoNew: [true, false, false, false, false],
        hxSelectInfoNew: [false, false, false, false, false, false],
        ztSelectInfoNew: [false, false, false],
        jjfsSelectInfoNew:[true,false],
        jfztSelectInfoNew:[false,false],
        priceSelectInfoNew: {
            totalPrice: 1001,
            totalPriceShow: '不限'
        },
        unitPriceSelectInfoNew: {
            unitPrice:100001,
            unitPriceShow:'不限'
        },
        //租房
        zffsSelectInfoZf:[false,false],
        priceSelectInfoZf:{
            totalPrice: 10001,
            totalPriceShow: '不限'
        },
        hxSelectInfoZf:[false, false, false, false],
        lySelectInfoZf:[false,false],
        //装修
        // lySelectInfoZx:[false,false],
        // lxSelectInfoZx:[false, false, false, false],
        // jwSelectInfoZx:[false, false, false, false, false, false,false],
        // sgfsSelectInfoZx:[false,false]
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '搜索'
        })
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
    },
    //切换tab
    handleTabChange: function (e) {
        this.setData({
            selectedId: e.detail
        })
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    //搜索
    searchChange: function (e) {

    },
    searchDone: function (e) {

    },
    //二手房
    //总价选择时以1000w为界 >1000的设定为不限价
    changaTotalPrice: function (e) {
        var totalSliderVal = e.detail.value;
        this.setData({
            priceSelectInfo: {
                totalPrice: totalSliderVal,
                totalPriceShow: totalSliderVal > 1000 ? '不限' : totalSliderVal + '万'
            }
        })
    },
    changePriceInput: function (e) {
        var totalSliderVal = e.detail.value.trim();
        var totalSliderValArr = totalSliderVal.split('万');

        if (totalSliderValArr.length > 2 || isNaN(parseInt(totalSliderValArr[0])) || totalSliderValArr[0] > 1000) {
            totalSliderVal = 1001;
        }
        this.setData({
            priceSelectInfo: {
                totalPrice: totalSliderVal,
                totalPriceShow: totalSliderVal > 1000 ? '不限' : totalSliderVal + '万'
            }
        })
    },
    //户型选择
    toggleHxSelect: function (e) {
        var hxIndex = e.currentTarget.dataset.idx;
        var hxSelect = this.data.hxSelectInfo;

        hxSelect[hxIndex] = !hxSelect[hxIndex];

        // if (hxIndex == 5 && !hxSelect[5]) {
        //     hxSelect = [false, false, false, false, false, true];
        // } else {
        //     hxSelect[5] = false;
        //     hxSelect[hxIndex] = !hxSelect[hxIndex];
        // }
        this.setData({
            hxSelectInfo: hxSelect
        })
    },
    //盘型选择
    togglexqpSelect: function (e) {
        var xqpIndex = e.currentTarget.dataset.idx;
        var xqpSelect = this.data.xqpSelectInfo;

        xqpSelect[xqpIndex] = !xqpSelect[xqpIndex];
        this.setData({
            xqpSelectInfo: xqpSelect
        })
    },
    //朝向选择
    toggleCxSelect: function (e) {
        var cxIndex = e.currentTarget.dataset.idx;
        var cxSelect = this.data.cxSelectInfo;

        cxSelect[cxIndex] = !cxSelect[cxIndex];
        this.setData({
            cxSelectInfo: cxSelect
        })
    },
    //特色
    toggleTsSelect: function (e) {
        var tsIndex = e.currentTarget.dataset.idx;
        var tsSelect = this.data.tsSelectInfo;

        tsSelect[tsIndex] = !tsSelect[tsIndex];
        this.setData({
            tsSelectInfo: tsSelect
        })

    },
    //区域
    toggleQySelect: function (e) {
        var qyIndex = e.currentTarget.dataset.idx;
        var qySelect = this.data.qySelectInfo;

        qySelect[qyIndex] = !qySelect[qyIndex];
        this.setData({
            qySelectInfo: qySelect
        })

    },
    //搜索
    beginSearch: function () {

    },
    //新房
    //房屋类型
    toggleLxSelect: function (e) {
        var lxIndex = e.currentTarget.dataset.idx;
        var lxSelect = this.data.lxSelectInfoNew;

        lxSelect[lxIndex] = !lxSelect[lxIndex];
        this.setData({
            lxSelectInfoNew: lxSelect
        })
    },
    //户型
    toggleHxSelectNew: function (e) {
        var hxIndex = e.currentTarget.dataset.idx;
        var hxSelect = this.data.hxSelectInfoNew;

        hxSelect[hxIndex] = !hxSelect[hxIndex];

        // if (hxIndex == 5 && !hxSelect[5]) {
        //     hxSelect = [false, false, false, false, false, true];
        // } else {
        //     hxSelect[5] = false;
        //     hxSelect[hxIndex] = !hxSelect[hxIndex];
        // }
        this.setData({
            hxSelectInfoNew: hxSelect
        })
    },
    //销售状态
    toggleZtSelect: function (e) {
        var ztIndex = e.currentTarget.dataset.idx;
        var ztSelect = this.data.ztSelectInfoNew;

        ztSelect[ztIndex] = !ztSelect[ztIndex];
        this.setData({
            ztSelectInfoNew: ztSelect
        })
    },
    //计价类型
    toggleJjfsSelect:function (e) {
        var jjIndex =e.currentTarget.dataset.idx;
        var jjSelect=this.data.jjfsSelectInfoNew;

        if(jjIndex==0&&!jjSelect[jjIndex]){
            jjSelect = [true,false];
        }
        if(jjIndex==1&&!jjSelect[jjIndex]){
            jjSelect=[false,true];
        }
        this.setData({
            jjfsSelectInfoNew:jjSelect
        })

    },
    changePriceInputNew: function (e) {
        var totalSliderVal = e.detail.value.trim();
        var totalSliderValArr = totalSliderVal.split('万');

        if (totalSliderValArr.length > 2 || isNaN(parseInt(totalSliderValArr[0])) || totalSliderValArr[0] > 1000) {
            totalSliderVal = 1001;
        }
        this.setData({
            priceSelectInfoNew: {
                totalPrice: totalSliderVal,
                totalPriceShow: totalSliderVal > 1000 ? '不限' : totalSliderVal + '万'
            }
        })
    },
    //总价选择时以1000w为界 >1000的设定为不限价
    changaTotalPriceNew: function (e) {
        var totalSliderVal = e.detail.value;
        this.setData({
            priceSelectInfoNew: {
                totalPrice: totalSliderVal,
                totalPriceShow: totalSliderVal > 1000 ? '不限' : totalSliderVal + '万'
            }
        })
    },
    //单价
    changeUnitPriceInputNew: function (e) {
        var totalSliderVal = e.detail.value.trim();
        var totalSliderValArr = totalSliderVal.split('元');

        if (totalSliderValArr.length > 2 || isNaN(parseInt(totalSliderValArr[0])) || totalSliderValArr[0] > 100000) {
            totalSliderVal = 100001;
        }
        this.setData({
            unitPriceSelectInfoNew: {
                unitPrice: totalSliderVal,
                unitPriceShow: totalSliderVal > 100000 ? '不限' : totalSliderVal + '元'
            }
        })
    },
    //单价选择时以100000为界 >100000的设定为不限价
    changaUnitPriceNew: function (e) {
        var totalSliderVal = e.detail.value;
        this.setData({
            unitPriceSelectInfoNew: {
                unitPrice: totalSliderVal,
                unitPriceShow: totalSliderVal > 100000 ? '不限' : totalSliderVal + '元'
            }
        })
    },
    //交付
    toggleJfztSelect:function (e) {
        var sIndex = e.currentTarget.dataset.idx;
        var sSelect = this.data.jfztSelectInfoNew;
        sSelect[sIndex] = !sSelect[sIndex];
        this.setData({
            jfztSelectInfoNew:sSelect
        })
    },
    //租房方式
    toggleZffsSelect:function (e) {
        var fsIndex = e.currentTarget.dataset.idx;
        var fsSelect = this.data.zffsSelectInfoZf;

        fsSelect[fsIndex] = !fsSelect[fsIndex];
        this.setData({
            zffsSelectInfoZf:fsSelect
        })
    },
    changeZjInput:function (e) {
        var totalSliderVal = e.detail.value.trim();
        var totalSliderValArr = totalSliderVal.split('元');

        if (totalSliderValArr.length > 2 || isNaN(parseInt(totalSliderValArr[0])) || totalSliderValArr[0] > 10000) {
            totalSliderVal = 10001;
        }
        this.setData({
            priceSelectInfoZf: {
                totalPrice: totalSliderVal,
                totalPriceShow: totalSliderVal > 10000 ? '不限' : totalSliderVal + '元'
            }
        })
    },
    changaZjPrice:function (e) {
        var totalSliderVal = e.detail.value;
        this.setData({
            priceSelectInfoZf: {
                totalPrice: totalSliderVal,
                totalPriceShow: totalSliderVal > 10000 ? '不限' : totalSliderVal + '元'
            }
        })
    },
    toggleZfHxSelect:function (e) {
        var hxIndex = e.currentTarget.dataset.idx;
        var hxSelect = this.data.hxSelectInfoZf;

        hxSelect[hxIndex] = !hxSelect[hxIndex];
        this.setData({
            hxSelectInfoZf:hxSelect
        })
    },
    toggleZflySelect:function (e) {
        var sIndex = e.currentTarget.dataset.idx;
        var sSelect = this.data.lySelectInfoZf;

        sSelect[sIndex] = !sSelect[sIndex];
        this.setData({
            lySelectInfoZf:sSelect
        })
    },
    //装修
    // toggleZxlySelect:function (e) {
    //     var sIndex = e.currentTarget.dataset.idx;
    //     var sSelect = this.data.lySelectInfoZx;
    //     sSelect[sIndex] = !sSelect[sIndex];
    //     this.setData({
    //         lySelectInfoZx:sSelect
    //     })
    // },
    // toggleZxlxSelect:function (e) {
    //     var sIndex = e.currentTarget.dataset.idx;
    //     var sSelect = this.data.lxSelectInfoZx;
    //     sSelect[sIndex] = !sSelect[sIndex];
    //     this.setData({
    //         lxSelectInfoZx:sSelect
    //     })
    // },
    // toggleZxjwSelect:function (e) {
    //     var sIndex = e.currentTarget.dataset.idx;
    //     var sSelect = this.data.jwSelectInfoZx;
    //     sSelect[sIndex] = !sSelect[sIndex];
    //     this.setData({
    //         jwSelectInfoZx:sSelect
    //     })
    // },
    // toggleSgfsSelect:function (e) {
    //     var sIndex = e.currentTarget.dataset.idx;
    //     var sSelect = this.data.sgfsSelectInfoZx;
    //     sSelect[sIndex] = !sSelect[sIndex];
    //     this.setData({
    //         sgfsSelectInfoZx:sSelect
    //     })
    // }

})
